// pages/hotCourse/hotCourse.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: 1,

        chooseMenu: null,
        categories: [],

        goodsType: 'all',
        filterType: 'all',
        sortPrice: 0,

        page: 1,
        hasMore: true,
        data: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const type = options.type || '0';
        let title = options.title || "课程";

        if (type === "0") {
            title = "我的课程";
        } else if (type === "1") {
            title = "热门课程专题";
            this.data.filterType = 'popular';
        } else if (type === "2") {
            title = "免费课程专区";
            this.data.filterType = 'free';
        } else if (type === "3") {
            title = "新课预报";
            this.data.filterType = 'new';
        } else if (type === "4") {
            title = "精华付费课程";
            this.data.filterType = 'pay';
        }

        wx.setNavigationBarTitle({
            title: title,
        });

        this.data.type = type;
        this.setData({
            type: type
        });

        this.onPullDownRefresh();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.loadCategories();
        this.loadData(1).finally(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (!this.data.hasMore) return;

        const page = this.data.page + 1;
        this.loadData(page);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        const image = "";
        return {
            imageUrl: image,
            path: `pages/course/index/index`
        }
    },

    // 加载分类信息
    loadCategories: function () {
        return wx.http.get('api/curriculum.category/index').then((res) => {
            const categories = res.data.data.list;
            this.setData({
                chooseMenu: this.data.chooseMenu,
                categories: categories
            });
            return categories;
        }, (err) => {
            console.warn("api/curriculum.category/index", err)
        });
    },

    // 加载数据
    loadData: function (page = 1) {
        const chooseMenu = this.data.chooseMenu;
        const categoryId = chooseMenu ? chooseMenu.category_id : '';

        const url = this.data.type === '0' ? 'api/user.curriculum/lists' : 'api/curriculum/lists';
        return wx.http.get(url, {
            page: page,
            type: this.data.filterType,
            sortType: this.data.goodsType,
            sortPrice: this.data.sortPrice,
            search: '',
            course_id: '',
            category_id: categoryId,
        }).then((res) => {
            res = res.data.data.list;
            const hasMore = res.data.length >= res.pre_page;

            if (this.data.type === '0') {
                res.data.forEach(it => {
                    const goods = it.goods[0];
                    delete it.goods[0];

                    it.curriculum_id = goods.goods_id;
                    it.curriculum_name = goods.goods_name;
                    it.image = [goods.image];
                });
            }

            const data = page > 1 ? this.data.data.concat(res.data) : res.data;
            this.setData({
                page: page,
                hasMore: hasMore,
                data: data,
            });
            return res.data;
        }, (err) => {
            console.warn("api/curriculum/lists", err)
        });
    },

    // 左侧分类切换
    goodsMenuTap: function (e) {
        const index = e.currentTarget.dataset.index;
        const menu = this.data.categories[index];
        this.data.chooseMenu = menu;
        this.setData({
            chooseMenu: menu
        });
        this.loadData(1);
    },
});
